<template lang="">
    <div>
        <van-field label="标题" v-model="form.title"/>
        <van-field label="价格" v-model="form.price"/>
        <select v-model="form.city_id">
            <option :value="i.id" v-for="i in citylist">{{i.name}}</option>
        </select>
        <van-uploader :after-read="upload"/>
        <van-button @click="add">提交</van-button>
    </div>
</template>

<script>
export default {
    data(){
        return{
            form:{pic:'',city_id:0},
            citylist:[]
        }
    },
    methods:{
        upload(file){
            //图片上传操作
            let formdata = new FormData()
            formdata.append('file',file.file)
            formdata.append('cityid',this.form.city_id)
            this.axios.post('upload',formdata).then(res=>{
                if(res.data.code == 200){
                    alert(res.data.url)
                    this.form.pic = res.data.url
                }
            })
        },
        getcitylist(){
            this.axios.get('citym').then(res=>{
                if(res.data.code == 200){
                    this.citylist = res.data.list
                    console.log(this.citylist)
                    //默认选中第一个
                    this.form.city_id = this.citylist[0]['id']
                }
                
            })
        },
        add(){
            this.axios.post('prom',this.form).then(res=>{
                alert('添加成功')
                this.form = {pic:'',city_id:0}
            })
        }
    },
    mounted(){
        this.getcitylist()
    }
}
</script>

<style>

</style>